<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="公司名称" prop="companyName" />
      <el-table-column label="申请部门" prop="applicationDepartment" />
      <el-table-column label="申请日期" prop="applicationDate" />
      <el-table-column label="文件名称" prop="fileName" />
      <el-table-column label="文件编号" prop="fileNumber" />
      <el-table-column label="版本" prop="version" />
      <el-table-column label="操作" align="center" width="230">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleView(row)">
            查看
          </el-button>
          <el-button type="success" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="80%">
      <xdfeizhi-form
        ref="xdfeizhiForm"
        :mode="dialogMode"
        :form-data="formData"
        @cancel="handleDialogCancel"
        @submit="handleDialogSubmit"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getXdfeizhiPage, removeXdfeizhi } from '@/api/xdfeizhi'
import Pagination from '@/components/Pagination'
import XdfeizhiForm from './components/XdfeizhiForm'

export default {
  name: 'XdfeizhiList',
  components: { Pagination, XdfeizhiForm },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        keyword: ''
      },
      dialogVisible: false,
      dialogMode: 'view', // view, create, edit
      dialogTitle: '',
      formData: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getXdfeizhiPage(this.listQuery)
        this.list = res.data.records
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    async handleView(row) {
      this.$router.push({
        path: '/main/biao/xdfeizhi',
        query: {
          id: row.id,
          mode: 'view'
        }
      })
    },
    handleCreate() {
      this.$router.push({
        path: '/main/biao/xdfeizhi',
        query: { mode: 'create' }
      })
    },
    async handleUpdate(row) {
      this.$router.push({
        path: '/main/biao/xdfeizhi',
        query: {
          id: row.id,
          mode: 'edit'
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await removeXdfeizhi(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      })
    },
    handleDialogCancel() {
      this.dialogVisible = false
    },
    handleDialogSubmit() {
      this.dialogVisible = false
      this.getList()
    }
  }
}
</script> 